package gentle.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import gentle.base.SqlMapper;
import gentle.base.StringUtil;
import gentle.entity.*;
import gentle.mapper.TcRevisitDao;
import gentle.service.ITcRevisitService;
import gentle.support.DateTimeKit;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class TcRevisitServiceImpl implements ITcRevisitService {

    @Autowired
    private TcRevisitDao dao;

    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<TcRevisitInfo> selectPageListByRevisit(Page<TcRevisitInfo> page, SearchTcRevisitInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        String tableHead = searchInfo.getAbbreviation();
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getRevisitStatus())) {
            params.put("revisitStatus", searchInfo.getRevisitStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartRevisitTime())) {
            params.put("startRevisitTime", searchInfo.getStartRevisitTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndRevisitTime())) {
            params.put("endRevisitTime", searchInfo.getEndRevisitTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartUpdateTime())) {
            params.put("startUpdateTime", searchInfo.getStartUpdateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndUpdateTime())) {
            params.put("endUpdateTime", searchInfo.getEndUpdateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFollowStatus())) {
            params.put("followStatus", searchInfo.getFollowStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getConsumptionStatus())) {
            params.put("consumptionStatus", searchInfo.getConsumptionStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getRevisit())) {
            params.put("revisit", searchInfo.getRevisit());
        }
        params.put("outpatientName", searchInfo.getOutpatientName());
        params.put("tableName", tableHead +"customer_info");
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.selectPageListByRevisit(page, params);
    }

    @Override
    public int getQueryPagesRevisitCount(Page<TcRevisitInfo> page, SearchTcRevisitInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        String tableHead = searchInfo.getAbbreviation();
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getRevisitStatus())) {
            params.put("revisitStatus", searchInfo.getRevisitStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartRevisitTime())) {
            params.put("startRevisitTime", searchInfo.getStartRevisitTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndRevisitTime())) {
            params.put("endRevisitTime", searchInfo.getEndRevisitTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartUpdateTime())) {
            params.put("startUpdateTime", searchInfo.getStartUpdateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndUpdateTime())) {
            params.put("endUpdateTime", searchInfo.getEndUpdateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFollowStatus())) {
            params.put("followStatus", searchInfo.getFollowStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getConsumptionStatus())) {
            params.put("consumptionStatus", searchInfo.getConsumptionStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getRevisit())) {
            params.put("revisit", searchInfo.getRevisit());
        }
        params.put("outpatientName", searchInfo.getOutpatientName());
        params.put("tableName", tableHead +"customer_info");
        return dao.getQueryPagesRevisitCount(page, params);
    }

    @Override
    public List<TongJiInfo> total(String reach, String startRevisitTime, String startTime, String endTime, String startUpdateTime, String endUpdateTime, String followStatus, String consumptionStatus,
                           String endRevisitTime, String revisit, String outpatientName, String abbreviation){
        String ctable=abbreviation+"customer_info";
        String contitionInfo = " and t.outpatient_name='"+outpatientName+"' ";
        if (StringUtils.isNotBlank(startRevisitTime)) {
            contitionInfo += " and t.revisit_time >='" + startRevisitTime + "' ";
        }
        if (StringUtils.isNotBlank(endRevisitTime)) {
            contitionInfo += " and t.revisit_time <='" + endRevisitTime + "' ";
        }
        if (StringUtils.isNotBlank(startTime)) {
            contitionInfo += " and t.create_time >='" + startTime + "' ";
        }
        if (StringUtils.isNotBlank(endTime)) {
            contitionInfo += " and t.create_time <='" + endTime + "' ";
        }
        if (StringUtils.isNotBlank(startUpdateTime)) {
            contitionInfo += " and t.update_time >='" + startUpdateTime + "' ";
        }
        if (StringUtils.isNotBlank(endUpdateTime)) {
            contitionInfo += " and t.update_time <='" + endUpdateTime + "' ";
        }
        if (StringUtils.isNotBlank(revisit)) {
            contitionInfo += " and t.revisit='" + revisit + "' ";
        }
        if (StringUtils.isNotBlank(followStatus)) {
            contitionInfo += " and t.follow_status='" + followStatus + "' ";
        }
        if (StringUtils.isNotBlank(consumptionStatus)) {
            contitionInfo += " and t.consumption_status='" + consumptionStatus + "' ";
        }
        if (StringUtils.isNotBlank(reach)) {
            contitionInfo += "   and (t1.customer_name like CONCAT('%','" + reach + "','%') or t1.customer_phone like CONCAT('%','" + reach + "','%') or " +
                    "            t1.wechat_number like CONCAT('%','" + reach + "','%') or t1.medical_record_number like CONCAT('%','" + reach + "','%') " +
                    "            or t1.customer_phone2 like CONCAT('%','" + reach + "','%') or t1.szm like CONCAT('%','" + reach + "','%') or t.tc_name like CONCAT('%','" + reach + "','%')) ";
        }
        contitionInfo += " group by t.id";
        String sql = "select count(*) as countNum from (select t.id from return_tc_visit_info t," + ctable + " t1 where t.customer_id = t1.id " + contitionInfo + ") t " +
                " union all select count(*) as countNum from (select t.id from return_tc_visit_info t , " + ctable + " t1 where t.customer_id = t1.id and t.revisit_status ='1' " + contitionInfo + ") t" +

                " union all  select count(*) as countNum from (select t.id from return_tc_visit_info t , " + ctable + " t1 where t.customer_id = t1.id and t.revisit_status ='2' " + contitionInfo + ") t" +

                " union all  select count(*) as countNum from (select t.id from return_tc_visit_info t , " + ctable + " t1 where t.customer_id = t1.id and t.revisit_status ='3' " + contitionInfo + ") t" +

                " union all  select count(*) as countNum from (select t.id from return_tc_visit_info t , " + ctable + " t1 where t.customer_id = t1.id and t.revisit_status ='4' " + contitionInfo + ") t";
        List<TongJiInfo> tongJiInfoList1 = sqlMapper.selectList(sql, TongJiInfo.class);
        return tongJiInfoList1;
    }

    public List<TcRevisitsInfo> TcFollowTableList(String reservationsId,String abbreviation){
        String ctable=abbreviation+"customer_info";
        String sql = "select t.id as id,t.customer_id as customerId,t.reservations_id as reservationsId,t.revisit as revisit,t.revisit_time as revisitTime," +
                "t.update_time as updateTime,t.create_time as createTime,t.outpatient_name as outpatientName,t.revisit_content as revisitContent,t.tc_name as tcName,t.remarks as remarks," +
                "t.revisit_status as revisitStatus,t.create_name as createName,r.customer_name as customer_Name,r.arrival_store as arrivalStore,r.field_tc as fieldTc,t.consumption_status,t.follow_status from return_tc_visit_info t " +
                "left join " +ctable+ " r on t.customer_id = r.id where t.reservations_id='" + reservationsId + "' order by t.revisit_time desc";
        List<TcRevisitsInfo> list = sqlMapper.selectList(sql, TcRevisitsInfo.class);
        return list;
    }

    public int addTcContent2(String customerId,String followStatus, String consumptionStatus, String revisitContent, String revisitTime,
                             String remarks, String reservationsId, String revisit,String abbreviation, String userName, String outpatientName){
        String ctable=abbreviation+"customer_info";
        String sql = "insert into return_tc_visit_info (reservations_id,customer_id,revisit,revisit_time,create_time," +
                "outpatient_name,remarks,revisit_content,create_name,follow_status,consumption_status,revisit_status) values ('" + reservationsId
                + "','" + customerId + "','" + revisit + "','" + revisitTime + "','" + DateTimeKit.now()+ "','" + outpatientName
                + "','" + remarks + "','" + revisitContent + "','" + userName
                + "','" + followStatus + "','" + consumptionStatus + "',1)";
        String updsql = "update " + ctable + " set first_return_settings=1, follow_status='" + followStatus + "',consumption_status='" + consumptionStatus + "'," +
                "return_to_remark='" + userName + "' where reservations_id='" + reservationsId + "'";
        sqlMapper.update(updsql);
        return sqlMapper.insert(sql);
    }

    public int addTcContentOver(String customerId,String followStatus, String consumptionStatus, String revisitContent, String revisitTime,
                                String remarks, String reservationsId, String revisit,String abbreviation, String userName, String outpatientName){
        String ctable=abbreviation+"customer_info";
        String sql = "insert into return_tc_visit_info (reservations_id,customer_id,revisit,revisit_time,create_time," +
                "outpatient_name,remarks,revisit_content,create_name,follow_status,consumption_status,revisit_status,update_time) values ('" + reservationsId
                + "','" + customerId + "','" + revisit + "','" + revisitTime + "','" + DateTimeKit.now() + "','" + outpatientName
                + "','" + remarks + "','" + revisitContent + "','" + userName
                + "','" + followStatus + "','" + consumptionStatus + "',2,'" + DateTimeKit.now() + "')";
        String updsql = "update " + ctable + " set first_return_settings=1, follow_status='" + followStatus + "',consumption_status='" + consumptionStatus + "'," +
                "return_to_remark='" + userName + "' where reservations_id='" + reservationsId + "'";
        sqlMapper.update(updsql);
        return sqlMapper.insert(sql);
    }

    public int delete_tcFollow(String ids){
        String sql = "delete from return_tc_visit_info where id='" + ids + "'";
        return sqlMapper.delete(sql);
    }

    public int updateTcContent(String tcName, String createName,String followStatus, String consumptionStatus,String revisitTime,
                               String revisitContent, String tcVisitId, String remarks, String reservationsId,String revisit,String abbreviation){
        String ctable=abbreviation+"customer_info";
        String updatesql = "update " + ctable + " set follow_status='" + followStatus + "',consumption_status='" + consumptionStatus + "' where reservations_id='" + reservationsId + "'";
        String sql = "update return_tc_visit_info set tc_name='" + tcName + "',create_name='" + createName
                + "',revisit_time='" + revisitTime + "',revisit_content='" + revisitContent
                + "',remarks='" + remarks + "',revisit='" + revisit + "',follow_status='"+followStatus+"',consumption_status='"+consumptionStatus+"' where reservations_id='" + reservationsId + "' and id='" + tcVisitId + "'";
        sqlMapper.update(updatesql);
        return sqlMapper.update(sql);
    }

    public int updateTcContent1(String tcName, String createName,String followStatus, String consumptionStatus,String revisitTime,
                                String revisitContent, String tcVisitId, String remarks, String reservationsId,String revisit,String abbreviation){
        String ctable=abbreviation+"customer_info";
        String sql = "update return_tc_visit_info set tc_name='" + tcName + "',create_name='" + createName
                + "',revisit_time='" + revisitTime + "',revisit_content='" + revisitContent
                + "',remarks='" + remarks + "',revisit='" + revisit + "',revisit_status='2',update_time='"+DateTimeKit.now()+"',follow_status='"+followStatus+"',consumption_status='"+consumptionStatus+"' where reservations_id='" + reservationsId + "' and id='" + tcVisitId + "'";
        String updatesql = "update " + ctable + " set follow_status='" + followStatus + "',consumption_status='" + consumptionStatus + "' where reservations_id='" + reservationsId + "'";
        sqlMapper.update(updatesql);
        return sqlMapper.update(sql);
    }

    public int updateTcContent2(String tcName, String createName,String followStatus, String consumptionStatus,String revisitTime,
                                String revisitContent, String tcVisitId, String remarks, String reservationsId,String revisit,String abbreviation){
        String ctable=abbreviation+"customer_info";
        String sql = "update return_tc_visit_info set tc_name='" + tcName + "',create_name='" + createName
                + "',revisit_time='" + revisitTime + "',revisit_content='" + revisitContent
                + "',remarks='" + remarks + "',revisit='" + revisit + "',revisit_status=4,follow_status='"+followStatus+"',consumption_status='"+consumptionStatus+"' where reservations_id='" + reservationsId + "' and id='" + tcVisitId + "'";
        String updatesql = "update " + ctable+ " set follow_status='" + followStatus + "',consumption_status='" + consumptionStatus + "' where reservations_id='" + reservationsId + "'";
        sqlMapper.update(updatesql);
        return sqlMapper.update(sql);
    }

    public SfInfo getSfById(String id) {
        String sql = "select * from tc_follow_template where id='" + id + "'";
        SfInfo info = sqlMapper.selectOne(sql, SfInfo.class);
        return info;
    }

    public SfInfo getJgById(String id) {
        String sql = "select * from tc_follow_results_template where id='" + id + "'";
        SfInfo info = sqlMapper.selectOne(sql, SfInfo.class);
        return info;
    }

    @Override
    public int deleteSfById(String id) {
        String sql = "delete from tc_follow_template where id='" + id + "'";
        return sqlMapper.delete(sql);
    }

    @Override
    public int deleteJgById(String id) {
        String sql = "delete from tc_follow_results_template where id='" + id + "'";
        return sqlMapper.delete(sql);
    }
}